// gauge.js

// 通用仪表盘配置生成函数
function createGaugeOption(title, maxValue, unit = '') {
    return {
        series: [{
            name: 'Z410',
            type: 'gauge',
            center: ['50%', '50%'],
            min: 0,
            max: maxValue,
            splitNumber: 12,
            radius: '82%',
            axisLine: {
                lineStyle: {
                    color: [
                        [0.09, 'lime'],
                        [0.82, '#1e90ff'],
                        [1, '#ff4500']
                    ],
                    width: 3,
                    shadowColor: '#fff',
                    shadowBlur: 10
                }
            },
            axisLabel: { //刻度数字
                distance: 0,
                textStyle: {
                    fontWeight: 'bolder',
                    color: '#fff',
                    shadowColor: '#fff',
                    shadowBlur: 10,
                    fontSize: 8
                }
            },
            axisTick: {
                length: 0,
                lineStyle: {
                    color: 'auto',
                    shadowColor: '#fff',
                    shadowBlur: 10
                }
            },
            splitLine: {
                length: 10,
                lineStyle: {
                    width: 1,
                    color: '#fff',
                    shadowColor: '#fff',
                    shadowBlur: 10
                }
            },
            pointer: { //指针
                shadowColor: '#fff',
                shadowBlur: 5
            },
            title: { //仪表标题
                textStyle: {
                    fontWeight: 'bolder',
                    fontSize: 15,
                    fontStyle: 'italic',
                    color: '#fff',
                    shadowColor: '#fff',
                    shadowBlur: 10
                }
            },
            detail: { //数字显示信息
                backgroundColor: 'rgba(30,144,255,0.8)',
                borderWidth: 1,
                borderColor: '#fff',
                shadowColor: '#fff',
                shadowBlur: 5,
                width: 30,
                height: 12,
                offsetCenter: [0, '80%'],
                textStyle: {
                    fontWeight: 'bolder',
                    color: '#fff',
                    fontSize: 14
                }
            },
            data: [{
                value: 0,
                name: title,
            }]
        }]
    };
}

// 在全局作用域声明仪表盘对象
let gauges = null;

// 初始化仪表盘
function initGauges() {
     gauges = {
        speed: echarts.init(document.getElementById('gauge-speed')),
        yaw: echarts.init(document.getElementById('gauge-yaw')),
        roll: echarts.init(document.getElementById('gauge-roll')),
        pitch: echarts.init(document.getElementById('gauge-pitch'))
    };

    // 设置不同仪表的配置
    gauges.speed.setOption(createGaugeOption('Speed', 120));
    gauges.yaw.setOption(createGaugeOption('Yaw', 360));
    gauges.roll.setOption(createGaugeOption('roll', 180));
    gauges.pitch.setOption(createGaugeOption('pitch', 90));
}

// 更新仪表盘数据的方法
function updateGaugeData(vehicle_info) {
    if (!gauges) return; // 确保仪表盘已初始化
    // 1. 提取地速（单位：米/秒）
    const speed = vehicle_info['speed'];
    const yaw =vehicle_info['yaw'];
    const roll =vehicle_info['roll'];
    const pitch =vehicle_info['pitch'];
    // 更新每个仪表盘（示例字段，根据实际数据格式调整）
    // 3. 更新仪表盘
    gauges.speed.setOption(createGaugeOption('Speed', speed));
    gauges.yaw.setOption(createGaugeOption('Yaw', radToDeg(yaw)));
    gauges.roll.setOption(createGaugeOption('Roll', radToDeg(roll)));
    gauges.pitch.setOption(createGaugeOption('Pitch', radToDeg(pitch)));
}

// 欧拉角解析工具函数
function parseAttitude(str) {
    // 示例字符串格式："<Attitude: roll=0.12, pitch=-0.05, yaw=1.57>"
    const pattern = /roll=([-\d.]+),\s*pitch=([-\d.]+),\s*yaw=([-\d.]+)/;
    const matches = str.match(pattern);

    if (!matches || matches.length < 4) {
        console.error('姿态数据解析失败:', str);
        return {roll: 0, pitch: 0, yaw: 0};
    }

    return {
        roll: parseFloat(matches[1]),
        pitch: parseFloat(matches[2]),
        yaw: parseFloat(matches[3])
    };
}

// 弧度转角度（保留2位小数）
function radToDeg(radians) {
    return parseFloat((radians * 180 / Math.PI).toFixed(2));
}